<?php
# -------------------------- File Info: JavaScript
#
# Owner: Hasan Ozgan
#
# --------- TODOLIST -------
#
# 1) Use cache
# 2) Merge whitespaces
# 2) Maybe User Choosable Script Files For Template Page

class Core_JSAction extends View
{
	public $body;

	public function Load()
	{
		//compress css files
		ob_start ("ob_gzhandler");
      	header ("content-type: text/javascript; charset: UTF-8");
      	header ("cache-control: must-revalidate");
	    $offset = 60 * 60;
	    $expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";
		header ($expire);

		$host_path = APP_DIR."/root/".DOMAIN;
		$js_cache_file_name = "javascript.joycache";
		$js_cache_file_path = $host_path. "/cache/pages/".$js_cache_file_name;
		$js_folder = $host_path. "/code/scripts/js";
		$js_cache_file = "";

		if (!file_exists($js_cache_file_path) || IS_FLUSH)
		{
			$fp = fopen($js_cache_file_path, "w");

			$dirhand = opendir($js_folder);
		    while ($file = readdir($dirhand))
			{ 
				if ($file != "." && $file != ".." && is_jsfile($file)) 
				{
					$js_file =	"/********* [ $file ] *********/\n\n";
					$js_file .= file_get_contents($js_folder."/".$file);
					fwrite($fp, $js_file);
					$js_cache_file .= $js_file;
                } 
			}

			fclose($fp);
		}
		else
		{
			$js_cache_file = file_get_contents($js_cache_file_path);
		}

		$this->body = $js_cache_file;
	}

}

?>
